package pushserver

import (
	"fmt"
	"pushserver/connpool"
	"pushserver/msgqueue"
	"pushserver/protocol"
	"pushserver/serialize"
)

// 使用默认参数创建 listener/sender
func NewDefaultServer(onSSL bool) (sder *SenderScheduler, listener *Listenter, err error) {
	return NewServer("default", "default", "default", "default", onSSL)
}

func NewServer(poolType, queueType, serializer, protocolType string, onSSL bool) (sder *SenderScheduler, listener *Listenter, err error) {
	defer func() {
		if e := recover(); e != nil {
			sder = nil
			listener = nil
			err = fmt.Errorf("create msg server error :%s", e)
		}
	}()
	pl := connpool.CreatePool(poolType)
	qu := msgqueue.CreateQueue(queueType)
	// 初始化 消息收发代理
	proxy := &TcpProxy{
		Seri:    serialize.CreateSerializer(serializer),
		Proto:   protocol.CreatePro(protocolType),
		IsOnSSL: onSSL,
	}
	// 初始化监听器
	listener = NewListener(pl, proxy)
	// 初始化消息发送器
	sder = NewSender(pl, qu, proxy)
	// 设置上线消息触发
	listener.onConnect = sder.UpOnline
	return
}
